import request from '@/config/axios'
import { sm2 } from 'sm-crypto'
export interface UserVO {
  id: number
  username: string
  nickname: string
  deptId: number
  postIds: string[]
  email: string
  mobile: string
  sex: number
  avatar: string
  loginIp: string
  status: number
  remark: string
  loginDate: Date
  createTime: Date
}
export interface CheckPasswordRespVO {
  success: boolean
  msg: string
}
// 查询用户管理列表
export const getUserPage = (params: PageParam) => {
  return request.get({ url: '/system/user/page', params })
}

// 查询所有用户列表
export const getAllUser = () => {
  return request.get({ url: '/system/user/all' })
}

// 查询用户详情
export const getUser = (id: number) => {
  return request.get({ url: '/system/user/get?id=' + id })
}

// 新增用户
export const createUser = (data: UserVO) => {
  return request.post({ url: '/system/user/create', data })
}

// 修改用户
export const updateUser = (data: UserVO) => {
  return request.put({ url: '/system/user/update', data })
}

// 删除用户
export const deleteUser = (id: number) => {
  return request.delete({ url: '/system/user/delete?id=' + id })
}

// 导出用户
export const exportUser = (params) => {
  return request.download({ url: '/system/user/export', params })
}

// 下载用户导入模板
export const importUserTemplate = () => {
  return request.download({ url: '/system/user/get-import-template' })
}

// 用户密码重置
export const resetUserPwd = (id: number, password: string) => {
  const data = {
    id,
    password
  }
  return request.put({ url: '/system/user/update-password', data: data })
}

// 用户状态修改
export const updateUserStatus = (id: number, status: number) => {
  const data = {
    id,
    status
  }
  return request.put({ url: '/system/user/update-status', data: data })
}

// 获取用户精简信息列表
export const getSimpleUserList = (): Promise<UserVO[]> => {
  return request.get({ url: '/system/user/simple-list' })
}

// 查询会员列表
export const getMemberPage = (params: PageParam) => {
  return request.get({ url: '/system/member/page', params })
}

// 查询用户详情
export const getMember = (id: number) => {
  return request.get({ url: '/system/member/get?id=' + id })
}

// 新增用户
export const createMember = (data: UserVO) => {
  return request.post({ url: '/system/member/create', data })
}

// 修改用户
export const updateMember = (data: UserVO) => {
  return request.put({ url: '/system/member/update', data })
}

// 删除用户
export const deleteMember = (id: number) => {
  return request.delete({ url: '/system/member/delete?id=' + id })
}

// 导出用户
export const exportMember = (params) => {
  return request.download({ url: '/system/member/export', params })
}

// 用户密码重置
export const resetMemberPwd = (id: number, password: string) => {
  const data = {
    id: id,
    password: password
  }
  return request.put({ url: '/system/member/update-password', data: data })
}

// 用户状态修改
export const updateMemberStatus = (id: number, status: number) => {
  const data = {
    id,
    status
  }
  return request.put({ url: '/system/member/update-status', data: data })
}

// 获取用户精简信息列表
export const getSimpleMemberList = (): Promise<UserVO[]> => {
  return request.get({ url: '/system/member/list-all-simple' })
}

// 用户密码检查
export const checkPwd = (password: string): Promise<CheckPasswordRespVO> => {
  return request.get({ url: '/system/user/checkPwd?password=' + password })
}
//检查是否弱密码
export const checkWeakPwd = (password: string): Promise<CheckPasswordRespVO> => {
  return request.get({ url: '/system/user/checkWeakPwd?password=' + password })
}
// 检查密码过期警告天数
export const getNotifyChangePwd = () => {
  return request.get({ url: '/system/user/getNotifyChangePwd' })
}
// 检查密码是否需要强制修改
export const getNeedChangePwd = () => {
  return request.get({ url: '/system/user/getNeedChangePwd' })
}
//获取sm2公钥
export const getSm2PublicKey = () => {
  return request.get({ url: '/system/auth/get-client-public-key' })
}
//sm2加密
export const sm2Encrypt = async (data: string) => {
  const publicKey = await getSm2PublicKey()
  const cipherMode = 1
  const decryptData = sm2.doEncrypt(data, publicKey, cipherMode) // 加密结果
  return decryptData
}
